﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class QuanTri_NhapGVCN : System.Web.UI.Page
{
    SoLienLacDataContext db = new SoLienLacDataContext();
    SoLienLacDienTu cls = new SoLienLacDienTu();
    string MaLop;
    string MaNam;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Username"] == null || Session["Username"].ToString() == "")
        {
<<<<<<< .mine
            Response.Redirect("~/GiaoDienClient/TrangChu.aspx");
=======
            cls.LoadTenGV(ddlGV);
           
            cls.LoadNamHoc(ddlNam);
            cls.LoadddlKhoiHoc(ddlKhoi);
            LoadLengrvDSGVCN();
            lbtDel.Enabled = false;
            lbtLuu.Enabled = false;
            //cls.LoadLop(ddlLop);
            //ddlGV.Enabled = false;
           // btnSua.Enabled = false;
            //btnXoa.Enabled = false;
          //  lblTenLop.Enabled = false;
            lbtBack.Visible = false;
           // btnXem.Enabled = false;
>>>>>>> .r6
        }
        else
        {
            MaLop = Request.QueryString["MaLop"];
            MaNam = Request.QueryString["MaNam"];
            if (!IsPostBack)
            {
                cls.LoadTenGV(ddlGV);
                LoadLengrvDSGVCN();
                cls.LoadNamHoc(ddlNam);
                cls.LoadddlKhoiHoc(ddlKhoi);
                //cls.LoadLop(ddlLop);
                //ddlGV.Enabled = false;
                btnSua.Enabled = false;
                btnXoa.Enabled = false;
                lblTenLop.Enabled = false;
                btnBack.Enabled = false;
                // btnXem.Enabled = false;
            }
        }
       
    }
    void LoadLengrvDSGVCN()
    {
        if (MaLop!=null&&MaNam!=null)
        {
            var c = from p in db.Lop_Bans
                    where p.MaNam == int.Parse(MaNam) && p.MaLop == int.Parse(MaLop)
                    select new { p.MaLopBan,p.MaNam, p.NamHoc.TenNam, p.CanBoGiaoVien.TenCanBo, p.LopHoc.TenLop,p.TrangThaiGiangDay };
            grvDSGVCN.DataSource = c;
            grvDSGVCN.DataBind();
            ddlGV.Enabled = true;
            lbtBack.Visible = true;
        }
        else
        {
            Loadgrv();
            ddlGV.Enabled = false;
        }
    }

    //protected void btnSua_Click(object sender, EventArgs e)
    //{
       
    //   // lblTenLop.Enabled = true;
    //   // btnXem.Enabled = true;
    //   // lblTenLop.Text = "";
    //}
    protected void grvDSGVCN_SelectedIndexChanged(object sender, EventArgs e)
    {
      //  btnSua.Enabled = true;
        //btnXoa.Enabled = true;
      //  btnXem.Enabled = false;
        lbtLuu.Enabled = true;
        lbtDel.Enabled = true;
        ddlKhoi.Enabled = false;
        ddlNam.Enabled = false;
      //  lblTenLop.Enabled = false;
        ddlGV.Enabled = true;
            GridViewRow row=grvDSGVCN.SelectedRow;
            Label MaLop = (Label)row.FindControl("lblMaLopBan");
           // Label MaNam = (Label)grvDSGVCN.Rows[i].Cells[0].FindControl("lblMaNam");
            Lop_Ban lb = db.Lop_Bans.SingleOrDefault(p => p.MaLopBan == int.Parse(MaLop.Text));
            if (lb.MaCanBoGV != "" && lb.MaCanBoGV != null)
            {
                ddlGV.SelectedItem.Text = lb.CanBoGiaoVien.TenCanBo.ToString();
            }
            ddlNam.SelectedItem.Text = lb.NamHoc.TenNam.ToString();
            lblMaLop.Text = lb.LopHoc.TenLop.ToString();
            ddlKhoi.SelectedItem.Text = lb.LopHoc.KhoiHoc.TenKhoi.ToString();
    }
    //protected void btnXem_Click(object sender, EventArgs e)
    //{
    //    //var c = from p in db.Lop_Bans
    //    //        where p.MaNam == int.Parse(ddlNam.SelectedValue.ToString())
    //    //            && p.LopHoc.MaKhoi == int.Parse(ddlKhoi.SelectedValue.ToString())
    //    //        select new { p.MaLopBan, p.LopHoc.TenLop, p.MaNam, p.NamHoc.TenNam, p.CanBoGiaoVien.TenCanBo, p.TrangThaiGiangDay };
    //    //grvDSGVCN.DataSource = c;
    //    //grvDSGVCN.DataBind();
    //    //btnSua.Enabled = true;
    //    //btnXoa.Enabled = true;
    //}
    void Loadgrv()
    {

        var c = from p in db.Lop_Bans
                where p.MaNam == int.Parse(ddlNam.SelectedValue.ToString())
                    && p.LopHoc.MaKhoi == int.Parse(ddlKhoi.SelectedValue.ToString())
                select new { p.MaLopBan, p.LopHoc.TenLop, p.MaNam, p.NamHoc.TenNam, p.CanBoGiaoVien.TenCanBo, p.TrangThaiGiangDay };
        grvDSGVCN.DataSource = c;
        grvDSGVCN.DataBind();
    }
    //protected void btnXoa_Click(object sender, EventArgs e)
    //{
        

    //}
    bool KiemTra()
    {
        bool kt = false;
        var c = from t in db.Lop_Bans where t.TrangThaiGiangDay == true select t.MaCanBoGV;
        GridViewRow row = grvDSGVCN.SelectedRow;
        Label MaLop = (Label)row.FindControl("lblMaLopBan");
        Label MaNam = (Label)row.FindControl("lblMaNam");
        Lop_Ban lb = db.Lop_Bans.SingleOrDefault(p => p.MaLopBan == int.Parse(MaLop.Text)
                     && p.MaNam == int.Parse(MaNam.Text));
        foreach (var con in c)
        {

            if (ddlGV.SelectedValue.ToString() == con)
            {
                kt = false;
                ScriptManager.RegisterStartupScript(this,this.GetType(),"Alert","alert('Giao viên này đang chủ nhiệm');",true);
                lb.MaCanBoGV =null;
                lb.TrangThaiGiangDay = null;
                db.SubmitChanges();
            }
            if (ddlGV.SelectedValue.ToString() != con)
            {
                kt = true;
               
                lb.MaCanBoGV = ddlGV.SelectedValue;
                lb.TrangThaiGiangDay = true;
                db.SubmitChanges();
            }
        }
        return kt;
    }
    //protected void btnBack_Click(object sender, EventArgs e)
    //{
    //    Response.Redirect("QuanLyLop_Ban.aspx");
    //}
    protected void lbtLuu_Click(object sender, EventArgs e)
    {
        GridViewRow row = grvDSGVCN.SelectedRow;
        Label MaLop = (Label)row.FindControl("lblMaLopBan");
        // Label MaNam = (Label)grvDSGVCN.Rows[i].Cells[0].FindControl("lblMaNam");
        Lop_Ban lb = db.Lop_Bans.SingleOrDefault(p => p.MaLopBan == int.Parse(MaLop.Text));
        lb.MaCanBoGV = ddlGV.SelectedValue;
        lb.TrangThaiGiangDay = true;
        db.SubmitChanges();
        Loadgrv();
        ddlKhoi.Enabled = true;
        ddlNam.Enabled = true;
        lblMaLop.Text = "";
    }
    protected void lbtDel_Click(object sender, EventArgs e)
    {
        GridViewRow row = grvDSGVCN.SelectedRow;
        Label MaLop = (Label)row.FindControl("lblMaLopBan");
        Label MaNam = (Label)row.FindControl("lblMaNam");
        Lop_Ban lb = db.Lop_Bans.SingleOrDefault(p => p.MaLopBan == int.Parse(MaLop.Text)
                     && p.MaNam == int.Parse(MaNam.Text));
        lb.TrangThaiGiangDay = false; ;
        db.SubmitChanges();
        Loadgrv();

    }
    protected void lbtBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("QuanLyLop_Ban.aspx");
    }
}
